using System;
using DevExpress.Xpo;
using NUnit.Framework;
using Tests.ObjectModel;

namespace Tests
{
   [TestFixture]
   public class when_using_a_persistent_datastore
   {
      #region Test Preparation

      [SetUp]
      public void SetUp()
      {
         XpoDefault.DataLayer = TestCommon.NewPersistentDataLayer();
      }

      #endregion

      #region Tests

      [Test]
      public void data_can_be_stored_and_retrieved()
      {
         int customerId, orderId, contractId;
         using (var unitOfWork = new UnitOfWork())
         {
            var customer = new Customer(unitOfWork)
            {
               Name = "John Doe"
            };

            var contract = new Contract(unitOfWork)
            {
               Customer = customer,
               Date = new DateTime(2000, 11, 9).Date
            };

            var order = new Order(unitOfWork)
            {
               Customer = customer,
               Date = DateTime.Now.Date,
               Total = 987.12m,
               Ignored = 999
            };

            customer.AllOrders.Add(order);

            unitOfWork.CommitChanges();

            customerId = customer.Oid;
            contractId = contract.Oid;
            orderId = order.Oid;
         }


         using (var unitOfWork = new UnitOfWork())
         {
            var customer = unitOfWork.GetObjectByKey<Customer>(customerId);
            Assert.IsNotNull(customer);
            Assert.AreEqual(1, customer.Orders.Count);
            Assert.AreEqual(orderId, customer.Orders[0].Oid);
            Assert.AreEqual(1, customer.Contracts.Count);
            Assert.AreEqual(contractId, ((Contract) customer.Contracts[0]).Oid);
         }
      }

      #endregion
   }
}